<?php

/**
 * default actions.
 *
 * @package    sf_sandbox
 * @subpackage default
 * @author     олег
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{
		$this->form = new SearchForm();
	}
	public function executeMakesearch(sfWebRequest $request)
	{
		$search = trim($request->getParameter('search'));		

		if(strlen($search)>2)
		{
			$this->getUser()->setAttribute('search', $search);
			
			$search = str_replace(' ', '%', $search);
			$search = '%'.$search.'%';

			$this->page = $request->getParameter('page','1');

			$this->getUser()->setAttribute('last_searched_page',$this->page);

			$new_pager = News::search($search, $this->page);
			$art_pager = Article::search($search, $this->page);
			$rel_pager = Reliz::search($search, $this->page);
			$ev_pager = Event::search($search, $this->page);
			$tend_pager = Tender::search($search, $this->page);

			$res_num = 0;
			$res_num+=count($new_pager);
			$res_num+=count($art_pager);
			$res_num+=count($rel_pager);
			$res_num+=count($ev_pager);
			$res_num+=count($tend_pager);

			$this->getUser()->setAttribute('search_num',$res_num);
			
			$this->searched_news = $new_pager->getResults(); 
			$this->searched_arts = $art_pager->getResults();
			$this->searched_rels = $rel_pager->getResults();			
			$this->searched_evs = $ev_pager->getResults();			
			$this->searched_tend = $tend_pager->getResults();

		$this->maxpage = max($new_pager->getLastPage(),$art_pager->getLastPage(), $rel_pager->getLastPage(), $ev_pager->getLastPage(), $tend_pager->getLastPage());

		}
		else
		{
			$this->form = new SearchForm();
			$this->getUser()->setFlash('search_err','Строка для поиска не меньше 3х символов');
			$this->setTemplate('index');
		}

	}

	public function executeClearsession(sfWebRequest $request)
	{
		$this->getUser()->setAttribute('search', NULL);		
		$fromwhere = $_SERVER['HTTP_REFERER'];
		$this->redirect($fromwhere);
	}

	public function executeReturntolastsearch(sfWebRequest $request)
	{
			$search = $this->getUser()->getAttribute('search');
			$search = str_replace(' ', '%', $search);
			$search = '%'.$search.'%';

			$this->page = $this->getUser()->getAttribute('last_searched_page');

			$new_pager = News::search($search, $this->page);
			$art_pager = Article::search($search, $this->page);
			$rel_pager = Reliz::search($search, $this->page);
			$ev_pager = Event::search($search, $this->page);
			$tend_pager = Tender::search($search, $this->page);

			$res_num = 0;
			$res_num+=count($new_pager);
			$res_num+=count($art_pager);
			$res_num+=count($rel_pager);
			$res_num+=count($ev_pager);
			$res_num+=count($tend_pager);

			$this->getUser()->setAttribute('search_num',$res_num);
			
			$this->searched_news = $new_pager->getResults(); 
			$this->searched_arts = $art_pager->getResults();
			$this->searched_rels = $rel_pager->getResults();			
			$this->searched_evs = $ev_pager->getResults();			
			$this->searched_tend = $tend_pager->getResults();
		
		$this->maxpage = max($new_pager->getLastPage(),$art_pager->getLastPage(), $rel_pager->getLastPage(), $ev_pager->getLastPage(), $tend_pager->getLastPage());
		
		$this->setTemplate('makesearch');
	}
}
